package com.kiell.gulimall.search.thread;

import org.springframework.cache.annotation.Cacheable;

import java.util.concurrent.*;

/**
 * @Description:
 * @Created: with IntelliJ IDEA.
 * @author: kiell
 * @createTime: 2024年3月15日17:37:01
 **/
public class ThreadTestByMe {
    //线程池
//    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,    //核心线程数
            200,    //最大线程数量，控制资源并发
            10,    //存活时间
            TimeUnit.SECONDS,    //时间单位
            new LinkedBlockingDeque<>(  100000),    //任务队列，大小100000个
            Executors.defaultThreadFactory(),    //线程的创建工厂
            new ThreadPoolExecutor.AbortPolicy());    //拒绝策略

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //01
//        System.out.println("main......start...");
//        Thread01 thread01 = new Thread01();
//        thread01.start();
//        System.out.println("main......end...");

        //02
//        System.out.println("main......start...");
//        Thread02Runnable thread02 = new Thread02Runnable();
//        new Thread(thread02).start();
//        System.out.println("main......end...");

        //03
//        System.out.println("main......start...");
//        FutureTask futureTask = new FutureTask<>(new Thread03Callable());
//        new Thread(futureTask).start();
//        //可以拿到返回结果， 可以处理异常
//        Integer i = (Integer) futureTask.get();
//        System.out.println("main......end..."+i);

        //04
//        System.out.println("main......start...");
//        executor.execute(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("当前线程是："+Thread.currentThread());
//            }
//        });
//        System.out.println("main......end...");

        //05
        //异步
        System.out.println("main......start...");
//        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getName());
//            //阻塞等待
//            Integer i = 14 / 2;
//            System.out.println("运行结果:" + i);
//        }, executor);

//        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getName());
//            //阻塞等待
//            Integer i = 12 / 0;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, executor).whenComplete((res,exception)->{
//            System.out.println("异步任务完成....感知到返回值为:"+res+"异常:"+exception);
//        }).exceptionally(throwable -> {
//            return 0;
//        });

//        CompletableFuture<Integer> supplyAsync = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程:" + Thread.currentThread().getName());
//            //阻塞等待
//            Integer i = 12 / 2;
//            System.out.println("运行结果:" + i);
//            return i;
//        }, executor).handle((res,exception)->{
//            if (res != null){
//                return res * 2 ;
//            }
//            if (exception != null){
//                return 0;
//            }
//            return 0;
//        });
//        System.out.println("supplyAsync........"+supplyAsync.get());

//        CompletableFuture<Void>  runAsync= CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程:"+Thread.currentThread().getName());
//            int i = 10 / 2;
//            System.out.println("运行结果...."+i);
//        }, executor).thenRunAsync(() -> {
//            System.out.println("任务二启动了...");
//        },executor);
//
//        System.out.println("返回数据:");

        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一线程开始:" + Thread.currentThread().getName());
            int i = 12 / 2;
            System.out.println("任务一运行结束...." + i);
            return i;
        }, executor);

        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务二线程开始:" + Thread.currentThread().getName());
//            System.out.println("任务二运行结束....");
//            return "hello";
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务二运行结束....");
            return "hello";
        }, executor);

//        future01.runAfterBothAsync(future02,() -> {
//            System.out.println("任务三开始...");
//        });

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (res1, res2) -> {
//            System.out.println("任务一返回值:" + res1 + "任务二返回值:" + res2);
//
//            return res1 + (String) res2;
//        }, executor);

//        System.out.println("返回数据:"+future.get());

//
//        future01.runAfterEitherAsync(future02,() -> {
//        System.out.println("任务三线程开始:" + Thread.currentThread().getName());
//    },executor);
//        future02.acceptEitherAsync(future01,res ->{
//            System.out.println("任务三线程开始:" + Thread.currentThread().getName()+"拿到上次任务的结果:"+res);
//        },executor);

//        CompletableFuture<String> future = future02.applyToEitherAsync(future01, res -> {
//            System.out.println("任务三线程开始:" + Thread.currentThread().getName() + "拿到上次任务的结果:" + res);
//            return res + "t3";
//        }, executor);
//
//        System.out.println("返回数据:"+future.get());
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        });

        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        });

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }
            System.out.println("查询商品介绍");
            return "华为";
        });
        // 等待全部执行完
//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        allOf.get();
        // 只需要有一个执行完
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        anyOf.get();
        System.out.println("main....end....." + anyOf.get());

//        System.out.println("main......end...");
    }

    public static class Thread03Callable implements Callable{
        @Override
        public Object call() throws Exception {
            System.out.println("当前线程:"+Thread.currentThread().getName());
            //阻塞等待
            Integer i=14/2;
            System.out.println("运行结果:"+i);
            return i;
        }
    }
    public static class Thread02Runnable implements  Runnable{
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }
    }
    //继承 Thread类，重写run()方法
    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }
    }
}
